(pushnew "~/src/lispbuilder/lispbuilder-sdl/" asdf:*central-registry* :test #'equal)
(pushnew "~/src/lispbuilder/lispbuilder-sdl-mixer/" asdf:*central-registry* :test #'equal)
(asdf:oos 'asdf:load-op 'lispbuilder-sdl)

(defparameter *entities* ())


(defclass entity ()
  ((position 
    :accessor position-of
    :initarg :position
    :initform (list 0 0))
   (velocity 
    :accessor velocity-of
    :initarg :velocity
    :initform (list 0 0))
   (acceleration
    :accessor acceleration-of
    :initarg :acceleration
    :initform (list 0 0))))

(defgeneric update (entity time))

(defmethod update ((entity entity) time)
  (let* ((new-velocity (mapcar #'+ (velocity-of entity) (mapcar #'* (acceleration-of entity) (list time time))))
	 (new-position (mapcar #'+ (position-of entity) (mapcar #'* (velocity-of entity) (list time time)))))
    (setf (velocity-of entity) new-velocity)
    (setf (position-of entity) new-position)))

(defgeneric to-string (entity))

(defmethod to-string ((entity entity))
  (format nil "pos: ~{(~a, ~a)~}, vel: ~{(~a, ~a)~}, accel: ~{(~a, ~a)~}" 
	  (position-of entity)
	  (velocity-of entity)
	  (acceleration-of entity)))

(let ((entity (make-instance 'entity
	       :position '(5 5)
	       :velocity '(1 2)
	       :acceleration '(1 1))))
  (to-string entity)
  (update entity 1)
  (update entity 1)
  (update entity 1)
  (to-string entity))


(defmacro play (screenw screenh	fps
		&optional (title "")
		&body body)
  
  `(sdl:with-init ()
   (let ((screenw ,screenw)
	 (screenh ,screenh)
	 (fps ,fps))
     (sdl:window screenw screenh :title-caption ,title)
     (setf (sdl:frame-rate) ,fps)
     (sdl:with-events ()
       (:quit-event () t)
       (:key-down-event () (sdl:push-quit-event))
       (:idle () ,@body)))))

(let ((entity
       (make-instance 'entity 
		      :position '(20 20)
		      :velocity '(10 1)
		      :acceleration '(1 10))))

  (play 600 300 60 "First test"

    (sdl:clear-display sdl:*black*)
    (update entity (sdl:dt))
    (sdl:draw-box 
     (sdl:rectangle-from-midpoint-* (first (position-of entity)) (second (position-of entity)) 20 20)
     :color (sdl:color :r 123 :g 255 :b 123))


    (sdl:update-display)))